﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VectorCalculator
{

    [Icon("pack://application:,,,/VectorCalculator;component/Resources/Icon.png")]
    [Designer("VectorCalculator.Designer.VectorCalculatorServerCommandDesigner, VectorCalculator")]
    [Category("向量计算")]
    internal class ScalarMultiplication : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        [DisplayName("向量")]
        [Description("由浮点数组成的数组，可通过AI大模型生成，形如[0.112,0.9871,...]")]
        public object Number1Exp { get; set; }

        [FormulaProperty]
        [DisplayName("缩放参数Scalar")]
        [Description("倍数，支持整数和小数。")]
        public object Number2Exp { get; set; }

        [ResultToProperty]
        [DisplayName("将计算结果返回到变量")]
        public string ResultTo { get; set; } = "Vector";

        public override string ToString()
        {
            return "向量缩放（标量乘法）";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var num1 = await dataContext.EvaluateFormulaAsync(this.Number1Exp);
            var num2 = (await dataContext.EvaluateFormulaAsync(this.Number2Exp))?.ToString() ?? "1";

            var vector1 = Convertor.ToVector(num1);
            var scalar = float.Parse(num2);

            if (vector1.Length == 0)
                throw new ArgumentException("向量长度不能为0。");

            float[] result = CalcWithSIMD.Scale(vector1, scalar);

            dataContext.Parameters[ResultTo] = result;

            return new ExecuteResult();


        }
    }
}
